package com.dq.controller.sys;

import com.dq.annotation.aop.SysLog;
import com.dq.domain.sys.menu.SysMenu;
import com.dq.domain.sys.role.SysRoleSection;
import com.dq.exception.customException.AdminException;
import com.dq.controller.AbstractController;
import com.dq.controller.in.section.SysSectionQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.impl.BaseDaoImpl;


import com.dq.domain.sys.section.SysSection;
import com.dq.utils.BeanNullUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * @author cf
 */
@Api(description = "组织部门管理")
@RestController
@RequestMapping("/sys/section")
public class SysSectionController extends AbstractController {
    @ApiOperation("组织部门列表-不分页-非树形列表")
    @PostMapping("/queryAll")
    public Vo<SysSection.Vo> queryAll(@RequestBody SysSectionQuery sectionQuery) {
        Map<String, Object> query = sectionQuery.parseMap();
        if(StringUtils.isNotBlank(sectionQuery.getSimpleName())) {
            query.put("simpleName" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(sectionQuery.getSimpleName()));
            query.remove("simpleName");
        }
        if(StringUtils.isNotBlank(sectionQuery.getFullName())) {
            query.put("fullName" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(sectionQuery.getFullName()));
            query.remove("fullName");
        }

        query.put("sidx", "sort");
        query.put("order", "asc");

        List<SysSection> list = dao.find(dao.query2Hql(SysSection.class, query), query);

        return new Vo(VO.toVOCollection(list));
    }

    @ApiOperation("所有组织部门-树形列表")
    @PostMapping("/listTree")
    public Vo<SysSection.Vo> listTree(@RequestBody SysSectionQuery sectionQuery) throws AdminException {

        if(sectionQuery.getStatus() == null) {
            sectionQuery.setStatus(SysMenu.Status.on);
        }
        return sysSectionService.listTree(sectionQuery);
    }

    @ApiOperation("组织部门信息")
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:section:info")
    public Vo info(@PathVariable @ApiParam(value = "部门id") Long id) {
        SysSection section = dao.get(SysSection.class, id);

        SysSection.Vo vo = (SysSection.Vo) section.toVO();
        List<Long> roleIds = new ArrayList<>();
        List<SysRoleSection> roleSections = sysRoleSectionRepository.findByDeletedFalseAndSection(section);
        for (SysRoleSection roleSection : roleSections) {
            roleIds.add(roleSection.getSysRole().getId());
        }
        vo.setRoleIds(roleIds);

        return new Vo(vo);
    }

    @ApiOperation("保存组织部门")
    @SysLog("保存组织部门")
    @PostMapping("/save")
    @RequiresPermissions("sys:section:save")
    public Vo save(@RequestBody @Valid SysSection section) throws AdminException {

        return new Vo(sysSectionService.save(section).toVO());
    }
    @ApiOperation("修改组织部门")
    @SysLog("修改组织部门")
    @PostMapping("/update")
    @RequiresPermissions("sys:section:update")
    public Vo update(@RequestBody @Valid SysSection section) throws AdminException {
        SysSection get = dao.get(SysSection.class, section.getId());
        if (get == null || get.getId() == null) {
            throw new AdminException("组织部门不存在");
        }
        BeanUtils.copyProperties(section, get, BeanNullUtil.getNullPropertyNames(section));

        return new Vo(sysSectionService.update(get).toVO());
    }

    @ApiOperation("删除组织部门/门店")
    @SysLog("删除组织部门/门店")
    @GetMapping("/delete/{id}")
    @RequiresPermissions("sys:section:delete")
    public Vo delete(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        sysSectionService.delete(id);

        return new Vo(HttpStatus.OK);
    }

}
